﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.IO;

namespace LCTMoodle.Helper
{
    public class FakeController : Controller
    {
    }
    public static class ControllerHelper
    {
        //public static string RenderRazorViewToString(string partialViewName, object model, ControllerContext controllerContext, ViewDataDictionary viewData, TempDataDictionary tempData)
        //{
        //    viewData.Model = model;
        //    using (var sw = new StringWriter())
        //    {
        //        var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
        //        var viewContext = new ViewContext(controllerContext, viewResult.View, viewData, tempData, sw);
        //        viewResult.View.Render(viewContext, sw);
        //        viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
        //        return sw.GetStringBuilder().ToString();
        //    }
        //}
        public static string RenderRazorViewToString(string partialViewName)
        {
            var st = new StringWriter();
            var context = new HttpContextWrapper(HttpContext.Current);
            var routeData = new RouteData();
            var controllerContext = new ControllerContext(new RequestContext(context, routeData), new FakeController());
            var razor = new RazorView(controllerContext, partialViewName, null, false, null);
            razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(), new TempDataDictionary(), st), st);
            return st.ToString();
        }
        public static string RenderRazorViewToString(string partialViewName, object model)
        {
            var st = new StringWriter();
            var context = new HttpContextWrapper(HttpContext.Current);
            var routeData = new RouteData();
            var controllerContext = new ControllerContext(new RequestContext(context, routeData), new FakeController());
            var razor = new RazorView(controllerContext, partialViewName, null, false, null);
            razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), st), st);
            return st.ToString();
        }
        public static string RenderRazorViewToString(string partialViewName, object model, ViewDataDictionary viewData)
        {
            viewData.Model = model;

            var st = new StringWriter();
            var context = new HttpContextWrapper(HttpContext.Current);
            var routeData = new RouteData();
            var controllerContext = new ControllerContext(new RequestContext(context, routeData), new FakeController());
            var razor = new RazorView(controllerContext, partialViewName, null, false, null);
            razor.Render(new ViewContext(controllerContext, razor, viewData, null, st), st);
            return st.ToString();
        }
        public static string RenderRazorViewToString(string partialViewName, object model, TempDataDictionary tempData)
        {
            var st = new StringWriter();
            var context = new HttpContextWrapper(HttpContext.Current);
            var routeData = new RouteData();
            var controllerContext = new ControllerContext(new RequestContext(context, routeData), new FakeController());
            var razor = new RazorView(controllerContext, partialViewName, null, false, null);
            razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), tempData, st), st);
            return st.ToString();
        }


        //public static class RenderHelper
        //{
            /*
            public static string PartialView(Controller controller, string viewName, object model)
            {
                controller.ViewData.Model = model;

                using (var sw = new StringWriter())
                {
                    var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                    var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);

                    viewResult.View.Render(viewContext, sw);
                    viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);

                    return sw.ToString();
                }
            }
            */
        //}
    }
}